package com.mt.channel.service.model;

import java.util.List;
import java.util.Map;

import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import com.github.pagehelper.PageHelper;
import com.mt.channel.entity.ChannelMsgEntity;
import com.mt.channel.mapper.ChannelMsgEntityMapper;
import com.mt.common.PagerInfo;
import com.mt.common.ServiceResult;

@Component
public class ChannelMsgServiceModel {
    private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(ChannelMsgServiceModel.class);

    @Autowired
    private ChannelMsgEntityMapper channelMsgEntityMapper;

    /**
     * 查询渠道商信息列表
     *
     * @param params
     * @param pagerInfo
     * @return
     */
    public ServiceResult<List<ChannelMsgEntity>> selectList(Map<String, Object> params, PagerInfo<?> pagerInfo) {
        Assert.notNull(this.channelMsgEntityMapper, "Property 'channelMsgEntityMapper' is required.");
        ServiceResult<List<ChannelMsgEntity>> result = new ServiceResult<List<ChannelMsgEntity>>();
        try {
            if (pagerInfo != null) {
                PageHelper.startPage(pagerInfo.getPageIndex(), pagerInfo.getPageSize());
            }
            result.setResult(this.channelMsgEntityMapper.selectList(params));
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("Unknown error!");
            LOGGER.error("[ChannelMsgServiceModel][selectList]：query selectList occur exception", e);
        }
        return result;
    }

    /**
     * 查询渠道商信息
     *
     * @param params
     * @return
     */
    public ServiceResult<ChannelMsgEntity> selectInfo(Map<String, Object> params) {
        Assert.notNull(this.channelMsgEntityMapper, "Property 'channelMsgEntityMapper' is required.");
        ServiceResult<ChannelMsgEntity> result = new ServiceResult<ChannelMsgEntity>();
        try {
            result.setResult(this.channelMsgEntityMapper.selectByPrimaryKey(params));
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("Unknown error!");
            LOGGER.error("[ChannelMsgServiceModel][selectInfo]：query selectInfo occur exception", e);
        }
        return result;
    }

    /**
     * 根据主键修改渠道商信息
     *
     * @param channelMsgEntity
     * @return
     */
    public ServiceResult<Integer> updateRelate(ChannelMsgEntity channelMsgEntity) {
        Assert.notNull(this.channelMsgEntityMapper, "Property 'channelMsgEntityMapper' is required.");
        ServiceResult<Integer> result = new ServiceResult<Integer>();
        int id = 0;
        try {
            int save = this.channelMsgEntityMapper.updateByPrimaryKeySelective(channelMsgEntity);
            if (save > 0) {
                id = channelMsgEntity.getChannelMsgId();
            }
            result.setResult(id);
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("Unknown error!");
            LOGGER.error("[ChannelMsgServiceModel][updateByPrimaryKeySelective]:query update occur exception", e);
        }
        return result;
    }

    /**
     * 根据主键修逻辑删除道商信息
     *
     * @param channelMsgId
     * @return
     */
    public ServiceResult<Integer> doDeleteById(Integer channelMsgId) {
        Assert.notNull(this.channelMsgEntityMapper, "Property 'channelMsgEntityMapper' is required.");
        ServiceResult<Integer> result = new ServiceResult<Integer>();
        int save = 0;
        try {
            save = this.channelMsgEntityMapper.updateByPrimaryKey(channelMsgId);
            result.setResult(save);
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("Unknown error!");
            LOGGER.error("[ChannelMsgServiceModel][doDeleteById]:query doDeleteById occur exception", e);
        }
        return result;
    }

    /**
     * 增加渠道商通知
     * 
     * @param channelMsgEntity
     * @return
     */
    public ServiceResult<Integer> insert(ChannelMsgEntity channelMsgEntity) {
        Assert.notNull(this.channelMsgEntityMapper, "Property 'channelMsgEntityMapper' is required.");
        ServiceResult<Integer> result = new ServiceResult<Integer>();
        int id = 0;
        try {
            int save = this.channelMsgEntityMapper.insert(channelMsgEntity);
            if (save > 0) {
                id = channelMsgEntity.getChannelMsgId();
            }
            result.setResult(id);
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("Unknown error!");
            LOGGER.error("[ChannelMsgServiceModel][insert]：query insert occur exception", e);
        }
        return result;
    }

    public ServiceResult<Integer> insertList(List<ChannelMsgEntity> channelMsgEntityList) {
        Assert.notNull(this.channelMsgEntityMapper, "Property 'channelMsgEntityMapper' is required.");
        ServiceResult<Integer> result = new ServiceResult<Integer>();
        try {
            int save = this.channelMsgEntityMapper.insertList(channelMsgEntityList);
            result.setResult(save);
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("Unknown error!");
            LOGGER.error("[ChannelMsgServiceModel][insertList]：query insert occur exception", e);
        }
        return result;
    }

    public ServiceResult<Integer> updateList(Map<String, Object> params) {
        Assert.notNull(this.channelMsgEntityMapper, "Property 'channelMsgEntityMapper' is required.");
        ServiceResult<Integer> result = new ServiceResult<Integer>();
        try {
            int save = this.channelMsgEntityMapper.updateList(params);
            result.setResult(save);
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("Unknown error!");
            LOGGER.error("[ChannelMsgServiceModel][updateList]：query insert occur exception", e);
        }
        return result;
    }

    public ServiceResult<Integer> selectCount(Map<String, Object> params) {
        Assert.notNull(this.channelMsgEntityMapper, "Property 'channelMsgEntityMapper' is required.");
        ServiceResult<Integer> result = new ServiceResult<Integer>();
        try {
            int save = this.channelMsgEntityMapper.selectCount(params);
            result.setResult(save);
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("Unknown error!");
            LOGGER.error("[ChannelMsgServiceModel][selectCount]：query insert occur exception", e);
        }
        return result;
    }

}
